<!DOCTYPE html>
<html>

<head>


    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>批量录入信息</title>
    <#include "/header.html">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <script src="${request.contextPath}/statics/plugins/layui/layui.all.js"></script>
    <style>
        .table_box{
            margin-top: 20px;
        }
        .table_box table tr td,th{
            text-align: center;
        }
    </style>
</head>

<body>
<style>
    .bootstrap-select{
        width: 100% !important;
    }
</style>
<div id="index">
    <div class="layui-container " style="width: 100%;" id="indexModel">

        <div class="layui-row" style="margin-top: 10px;">
            <div class="layui-col-md2" style="margin-right: 10px;">
                <select class="selectpicker" id="siteType" title="请选择平台类型"  style="width:100%;">
                    <option value="1">日活</option>
                    <option value="2">安装APP</option>
                    <!--<option value="3">下载APP率</option>-->
                    <option value="4">域名成本</option>
                    <option value="5">竞价收入</option>
                </select>
            </div>
            <div class="layui-col-md3 ">
                <select class="selectpicker" id="siteSelect" title="请选择平台名称"  style="width:100%;">

                </select>
            </div>
            <div class="layui-col-md3" style="padding-left: 20px;">
                <input type="text" class="form-control" required  id="startTime"  placeholder="请输入日期"  autocomplete="off" >
            </div>
            <div class="layui-col-md3 " style="padding-left: 20px;">
                <a class="btn btn-primary" style="margin-left: 10px;" id="btnadd" onclick="enterGroupPage.addNewTr()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                <a class="btn btn-default" id="search" onclick="enterGroupPage.setData()">设置</a>
                <a class="btn btn-warning" id="backBtn" href="entering.html">返回</a>
                <a class="btn btn-info" id="saveBtn" onclick="enterGroupPage.saveAllData()">确定</a>
            </div>
        </div>
        <div class="table_box">
             <table class="table table-bordered">
                 <thead>
                     <tr>
                         <th>平台类型</th>
                         <th>平台名称</th>
                         <th>时间</th>
                         <th>数量</th>
                         <th>操作</th>
                     </tr>
                 </thead>
                 <tbody id="tbodyCon" class="table-striped table-hover">

                 </tbody>
             </table>
        </div>
    </div>
</div>
<script>
    $(function () {
        enterGroupPage.init();
    });
    var isAdded = false;
    var num = 0;
    var enterGroupPage = {
         init:function () {
             var _this = this;
             var laydate = layui.laydate;
             laydate.render({
                 elem: '#startTime',
                 value: new Date(),
                 showBottom: false,
                 position: 'fixed',
             });
             _this.getAllPlatformData();
         },
         getAllPlatformData:function () {
             $.ajax({
                 type : "get",
                 url : baseURL+"platform/findAllPlatform",
                 dataType : "json",
                 success: function(data) {
                     if(data.code==0){
                         var _html = ''
                         for(var i=0;i<data.platformList.length;i++){
                             _html = _html + '<option value="'+data.platformList[i].platformId+'">'+data.platformList[i].platformName+'</option>'
                         }
                         $('#siteSelect').html(_html);
                         $('#siteSelect').selectpicker('refresh');
                     }else{
                         layer.msg('数据获取失败');
                     }
                 },
                 error:function(){
                     statsData = false;
                     layer.msg('数据获取失败');
                 }
             });
         },
         addNewTr:function () {
                  var str = '';
                  var _this = this;
                  if(num>0){
                      _this.checkTrValComp();
                  }
                  if(!isAdded){
                      isAdded = true;
                      str = str + '<tr class="tr'+num+'"><td data-type="" class="typeVal"></td><td data-name="" class="nameVal"></td><td class="timeVal"></td><td>';
                      str = str + '<input class="numVal form-control" type="text" /></td>';
                      str = str + '<td> <a class="btn btn-danger" onclick="enterGroupPage.removeTr('+num+')">删除</a></td>';
                      $("#tbodyCon").append(str);
                      num++;
                  }
         },
        removeTr:function (no) {
             var className = '.tr'+no;
             $("#tbodyCon").find(className).remove();
             num--;
             isAdded = false;
        },
        setData:function () {
            var thisName = ".tr"+ num;
            var platformName = $("#siteSelect").find('option:selected').text();
            var platfromValue = $("#siteSelect").val();
            var siteType = $("#siteType").find('option:selected').text();
            var siteTypeValue = $("#siteType").val();
            var timeVal = $("#startTime").val();
            var parentNo =  $("#tbodyCon").children("tr:last-child");
            parentNo.find(".typeVal").html(siteType);
            parentNo.find(".typeVal").prop('data-type',siteTypeValue);
            parentNo.find(".nameVal").html(platformName);
            parentNo.find(".nameVal").prop('data-name',platfromValue);
            parentNo.find(".timeVal").html(timeVal);
        },
        checkTrValComp:function () {
            var thisTr = $("#tbodyCon").children("tr:last-child");
            var siteType = thisTr.find(".typeVal").html();
            var platformName = thisTr.find(".nameVal").html();
            var timeVal = thisTr.find(".timeVal").html();
            var numVal = thisTr.find(".numVal").val();
           // console.log(numVal)
            if(siteType && platformName && timeVal && numVal){
                isAdded = false;
            }else{
                alert("请设置本行完整信息后再新增");
                isAdded = true;
            }

        },
        saveAllData:function () {
             var _this = this;
             var data = _this.transfromData();
             if(data.inputType.length>0){
                 console.log(data);
             }
        },
        transfromData:function () {
            var _this = this;
            var params = {
                inputType:[],
                statisValue:[],
                platformId:[],
                statisDateStr:[]
            };
            _this.checkTrValComp();
            if(!isAdded){
                 $("#tbodyCon tr").map(function (item) {
                   var item1 =  $(this).find(".typeVal").prop("data-type");
                   var item2 =  $(this).find(".nameVal").prop("data-name");
                   var item3 =  $(this).find(".timeVal").text();
                   var item4 =  $(this).find(".numVal").val();
                   params.inputType.push(item1);
                   params.platformId.push(item2);
                   params.statisDateStr.push(item3);
                   params.statisValue.push(item4);
                });
            }
            return params;
        }
     }
</script>
</body>

</html>